/**
 * Copyright (c) 2009 Cerner Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Cerner Corporation - initial API and implementation
 */
package com.cerner.system.rest.jaas.cas;

import java.net.URI;
import java.net.URISyntaxException;

import javax.security.auth.callback.TextInputCallback;

/**
 * Implementation of the JAAS {@link javax.security.auth.callback.Callback}
 * interface that allows consumers of the {@link CasLoginModule} to
 * programmatically set the {@code casLoginUrl} at runtime. This callback
 * extends the established {@link TextInputCallback} to lower the entry barrier
 * for GUIs and console-based applications. Note though that a valid {@link URI
 * casLoginUrl} must be provided with either setter method.
 * 
 * @author Alex Horn
 * @since 1.0.0
 */
public class CasLoginUrlCallback extends TextInputCallback {

  private static final long serialVersionUID = 6655954570236412821L;

  private URI casLoginUrl;

  /**
   * Instantiate a new JAAS callback that allows users to enter the {@code
   * casLoginUrl}. The prompt must not be {@code null} or empty.
   * 
   * @param prompt human-readable message that asks for user input of the
   *          {@code casLoginUrl} setting
   */
  public CasLoginUrlCallback(String prompt) {
    super(prompt);
  }

  /**
   * Set the lexical representation of the {@code casLoginUrl}. It must have a
   * trailing slash.
   * 
   * @param casLoginUrl lexical URL representation of REST service that produces
   *          CAS ticket granting tickets
   * @throws CasFault if the {@code casLoginUrl} cannot be converted to a
   *           {@link URI} object
   */
  @Override
  public void setText(String casLoginUrl) {
    try {
      setCasLoginUrl(new URI(casLoginUrl));
    } catch (URISyntaxException e) {
      throw new CasFault("Unable to convert text input to casLoginUrl.", e);
    }
    super.setText(casLoginUrl);
  }

  /**
   * Set the {@code casLoginUrl}. It must have a trailing slash.
   * 
   * @param casLoginUrl URL of REST service that produces CAS ticket granting
   *          tickets
   */
  public void setCasLoginUrl(URI casLoginUrl) {
    this.casLoginUrl = casLoginUrl;
  }

  /**
   * Get the URL of REST service that produces CAS ticket granting tickets.
   * 
   * @return CAS context root with trailing slash
   */
  public URI getCasLoginUrl() {
    return this.casLoginUrl;
  }
}
